/********************General information******************************************
*Author: David Roberto Bellomo Gomez
*Date: 05/26/13
*File: Platform_Serial_Manager.cpp
**********************File description********************************************
*Version: 1.0
*Description: This file gives provides the appis that will be used by the application
all the  high level control  for the serial module will be managed by this appis.
*********************Change historial**********************************************
*Author:
*Date:
*Description of the change 
***********************************************************************************/
/*This include provides the inteface with the arduino appis*/
#include <Platform_Serial_Interface.h>
/*This includes provides de definitios for the manager*/
#include <Platform_serial_Manager.h>

/*Variable definitions*/

static ManagerState_Type g_vsB_SerialMngState = SeriaMng_NOINITstate;
PendingFlag_Type  PendingFlags = 0;


void SerialMng_Init(long l_pL_BaudRate){
  //TODO: Manage if Serial driver is not correctly managed. 
  if (SeriaMng_NOINITstate == g_vsB_SerialMngState ){
    //Serial Manager is not initialized
    Start_SerialCom(l_pL_BaudRate);
    //Serial Manager in IDLE state 
    g_vsB_SerialMngState = SerialMng_IDLEstate;
  }
  
}

boolean f_getBusState(void){
  if  GetAvailableBytes_SerialCom()
    return 0x01u
  else
    return 0x00u
} 
    
/********************************************
*******************************************/
void f_SerialTransmit(void){
  //TODO: Implement this function 
  
}
/********************************************
*******************************************/
void f_SerialRecieve(void){
  //TODO: implement this function
}
/********************************************
*******************************************/
void SerialMng_StMachine(void){
  ManagerState_Type l_vsB_CurrentMngState;
  ManagerState_Type l_vsB_NextMngState;
  if (SeriaMng_NOINITstate != g_vsB_SerialMngState){
    //Manager is in a valid state 
    l_vsB_CurrentMngState =  g_vsB_SerialMngState;
  
    switch(l_vsB_CurrentMngState){
      default:
            //TODO:Colocar error
            
            break;
      case SerialMng_IDLEstate:
            if(m_chekRQflag(/*PlaceMask*/)){//TODO: macro chakRQFlag   and mask 
              //Serial Request Pending
                m_setReadWritePendinFlag(f_getBusState());
                l_vsB_NextMngState = SerialMng_CHEKINGstate;
            }
            else
                l_vsB_NextMngState = l_vsB_CurrentMngState; //Remains in the IDLE state
            break;
      case SerialMng_CHEKINGstate:
              if m_CheckPendingFlag(TRANSMITPENDING)
                  //Transmition in progress
                l_vsB_NextMngState = SerialMng_TRANSMITstate;
              else
                if m_CheckPendingFlag(RECIVINGPENDING) 
                //Read in progress 
                l_vsB_NextMngState = SerialMng_RECIVINGstate;
                PendingFlags = 0; //Clear Pending flag
            break;      
      case SerialMng_TRANSMITstate:
                f_SerialTransmit(); //TODO: Serial Transmit
            break;
      case SerialMng_RECIVINGstate:
                 f_SerialRecieve(); //TODO: Serial Recive
            break;
      case SerialMng_STOPstate:
            break;
      
    }
    
    
  }
  else{
  }  //TODO: Insert error manage maybe a return with an error code.
  
  g_vsB_SerialMngState = l_vsB_NextMngState;
}





